package com.heatup.apt.common.util;

import java.net.HttpURLConnection;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class WeiXinUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(WeiXinUtil.class);
	
	/**
	 * 从wx下载图片上传到oss
	 * @param downloadUrl 微信服务器图片下载地址
	 * @return path  oss的图片地址
	 * @throws Exception
	 */
	public static String uploadImgFromWxToOSS(String downloadUrl) throws Exception {
        URL url = new URL(downloadUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        String contentType = conn.getContentType();
        String length = conn.getHeaderField("Content-Length");
        Integer l = Integer.parseInt(length);
        String path = OssFileUtil.uploadFileByIS(conn.getInputStream(), contentType, l, ".jpg");
        logger.info("stock_image_url path :" + path);  
        return path;
	}
	
	 public static void main(String[] args) {
		try{
			URL url = new URL("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=lg2gtLEO08Q_ZpXpduOBN5GqvTQuL4efyChqSKZk9pT3s1Hk2c25SSKy4bpPxxDChm7ymrHkHRgG99inqHpCWPU0-4_CQInJr-dwknBNhLzUdHrxjD9nJa_w9YqMIUUABWVbADAWRQ&media_id=aPLS-b28c5vuaWvuSTl0ri5ewDCMknppdYHFV-y6FT-b9FOkvF4dOci1TS6RFZDR");
	        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
	        conn.setDoOutput(true);
	        conn.setDoInput(true);
	        conn.setUseCaches(false);
	        String contentType = conn.getContentType();
	        String length = conn.getHeaderField("Content-Length");
	        Integer l = Integer.parseInt(length);
	        String path = OssFileUtil.uploadFileByIS(conn.getInputStream(), contentType, l, ".jpg");
	        System.out.println(path);
		} catch(Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
